home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / emac16as.arc / FILES.ASM < prev    next >
Assembly Source File  |  1990-04-01  |  6KB  |  269 lines

  1. ;History:194,1
  2. ;Thu Feb 22 23:41:33 1990 make buffer_modified into a bitmap.
  3. ;Tue Feb 13 19:21:28 1990 If a file being read is read-only, return that information.
  4. ;09-07-88 22:03:25 remove extern margin: abs
  5.     page    ,132
  6.  
  7.     .xlist
  8.  
  9.     include    memory.def
  10.  
  11.     .list
  12.  
  13. bufseg    segment    public
  14.     extrn    toptop: word
  15.     extrn    topbot: word
  16.     extrn    bottop: word
  17.     extrn    botbot: word
  18.     extrn    linecount: word
  19.     extrn    linesbefore: word
  20.     extrn    buffer_modified: byte
  21. bufseg    ends
  22.  
  23.  
  24. data    segment byte public
  25.  
  26.     extrn    textseg: word
  27.  
  28. data    ends
  29.  
  30.  
  31. code    segment byte public
  32.  
  33.     assume    cs:code, ds:data, es:data
  34.  
  35.     extrn    count_lines: near
  36.     extrn    read_mark$: near
  37.     extrn    adjust_marks_ins: near
  38.     extrn    paint_window: near
  39.     extrn    buffer_free: near
  40.  
  41.  
  42.     public    write_file
  43. write_file:
  44. ;enter with si->filename desired, al=mark.
  45. ;exit with al=0 if ok, al=1 if disk full, or al=2 if directory full.
  46.     push    si            ;save a pointer to the filename.
  47.  
  48.     push    ax            ;save the mark to write to.
  49.     mov    dx,si
  50.     mov    cx,0
  51.     mov    ah,3ch            ;create file.
  52.     int    21h
  53.     pop    bx            ;get the mark back
  54.     jc    write_2_5        ;if any problem, report dir full.
  55.     xchg    bx,ax            ;put the mark in ax, handle in bx.
  56.  
  57.     push    ds
  58.     mov    ds,textseg        ;read_mark$ assumes ds:textseg
  59.     push    bx
  60.     call    read_mark$
  61.     pop    bx
  62.     mov    dx,si            ;set the disk transfer address
  63.     mov    ah,40h            ;write out what they requested.
  64.     int    21h
  65.     pop    ds
  66.     jc    write_2_4        ;if any problems, disk full.
  67.  
  68.     cmp    ax,cx            ;did we write the whole thing?
  69.     jne    write_2_4        ;no - disk full.
  70.  
  71. ;close the file.
  72.     mov    ah,3eh            ;close file.
  73.     int    21h
  74.     jc    write_2_4        ;if any problems, say disk full.
  75.     pop    si
  76.     mov    al,0
  77.     ret
  78. write_2_4:
  79.     mov    ah,3eh            ;close file.
  80.     int    21h
  81.     pop    si
  82.     mov    dx,si            ;delete their file.
  83.     mov    ah,41h
  84.     int    21h
  85.     mov    al,1
  86.     ret
  87. write_2_5:
  88.     pop    si
  89.     mov    al,2
  90.     ret
  91.  
  92.  
  93.     public    read_file
  94. read_file:
  95. ;enter with si->filename desired.
  96. ;exit with al=0 if ok, al=1 if file too large, or al=2 if file not found.
  97.     mov    dx,si
  98.     mov    ax,4300h        ;get the files's attributes
  99.     int    21h
  100.     mov    bp,cx            ;remember the attributes here.
  101.  
  102.     mov    ax,3d00h        ;open for reading.
  103.     int    21h
  104.     jc    read_2_5
  105.     mov    bx,ax
  106.  
  107.     mov    ax,4202h        ;get the file's size.
  108.     mov    cx,0
  109.     mov    dx,cx
  110.     int    21h
  111.     mov    cx,ax            ;remember the file's size lobyte.
  112.  
  113.     or    dx,dx            ;is file too big?
  114.     jne    read_2_2        ;if it's more than one segment, it's too big.
  115.     mov    ax,textseg
  116.     call    buffer_free        ;ensure that the file will fit.
  117.     jc    read_2_2        ;it won't.
  118.  
  119. ;if the file's empty, don't try to read it in.
  120.     jcxz    read_2_7
  121.  
  122. ;seek to the beginning again.
  123.  
  124.     push    cx
  125.     mov    ax,4200h        ;rewind the file.
  126.     mov    cx,0
  127.     mov    dx,cx
  128.     int    21h
  129.     pop    cx
  130.  
  131. ;read the file in.
  132.  
  133.     push    ds
  134.     mov    ds,textseg
  135.     assume    ds:bufseg
  136.     mov    dx,topbot        ;read the file before the cursor.
  137.     mov    ah,3fh
  138.     int    21h
  139.     pop    ds
  140.     assume    ds:data
  141.     jc    read_2_4        ;problem with read - give up.
  142.  
  143.     cmp    cx,ax            ;compare the amount desired again the amount read.
  144.     pushf
  145.     mov    cx,ax            ;adjust for the amount read.
  146.     push    bx            ;preserve the file handle.
  147.     call    read_adjust
  148.     pop    bx
  149.     popf
  150.     jnz    read_2_4        ;if any not read, give up.
  151.  
  152.  
  153. ;close the file.
  154. read_2_7:
  155.     mov    ah,3eh            ;close the file.
  156.     int    21h
  157.     jc    read_2_4        ;if any trouble, give up.
  158.     mov    al,4            ;read-only file.
  159.     test    bp,1            ;is this a read-only file?
  160.     jne    read_2_exit        ;yes - return read-only.
  161.     mov    al,0            ;all okay.
  162.     jmp    short read_2_exit
  163. read_2_2:
  164.     mov    al,1            ;not enough memory.
  165.     jmp    short read_2_exit
  166. read_2_4:
  167.     mov    ah,3eh            ;close the file.
  168.     int    21h
  169.     mov    al,3            ;other read error.
  170.     jmp    short read_2_exit
  171. read_2_5:
  172.     mov    al,2            ;file not found.
  173. read_2_exit:
  174.     ret
  175.  
  176.  
  177. read_adjust:
  178. ;update topbot, line counters.
  179.     push    cx            ;save count
  180.     mov    ds,textseg
  181.     assume    ds:bufseg
  182.     mov    ax,cx
  183.     call    adjust_marks_ins
  184.     mov    di,topbot
  185.     push    es
  186.     pop    ds
  187.     assume    ds:data
  188.     pop    cx            ;get the count back.
  189.     push    cx
  190.     call    count_lines        ;count NEWLINES in text we just read in.
  191.     mov    ds,textseg
  192.     assume    ds:bufseg
  193.     add    linesbefore,bx        ;the text is before the cursor, so add
  194.     add    linecount,bx
  195.     or    buffer_modified,1
  196.     pop    cx            ;restore count
  197.     add    topbot,cx        ;add the amount of file that was read in.
  198.     push    es
  199.     pop    ds
  200.     assume    ds:data
  201.     call    paint_window
  202.     ret
  203.  
  204.  
  205.     public    compute_free
  206. compute_free:
  207. ;enter with dl=drive.
  208. ;exit with cy if invalid drive, or nc and:
  209. ;  ax=sectors/cluster,
  210. ;  bx=free clusters,
  211. ;  cx=bytes/sector,
  212. ;  dx=total clusters.
  213.     mov    ah,30h            ;get the version number
  214.     int    21h
  215.     cmp    al,2            ;al<2 for versions 1.?
  216.     jb    compute_free_5
  217.     mov    ah,36h            ;get disk stats for version 2.0+
  218.     int    21h            ;dl already set
  219.     cmp    ax,-1            ;valid drive?
  220.     jne    compute_free_4        ;yes.
  221.     push    ds
  222. compute_free_6:
  223.     pop    ds
  224.     stc
  225.     ret
  226. compute_free_5:
  227.     push    ds
  228.     mov    ah,1ch            ;get the fat.
  229.     int    21h
  230.     or    bx,bx
  231.     je    compute_free_6
  232.     mov    ah,0
  233.     push    ax            ;save the sectors/cluster
  234.     push    cx            ;save the bytes/sector
  235.     mov    cx,dx            ;we need the count in cx.
  236.     xor    ax,ax            ;start with no free clusters.
  237.     mov    si,2            ;start at the first cluster.
  238. compute_free_1:
  239.     mov    di,si
  240.     shr    di,1
  241.     add    di,si
  242.     mov    di,[bx+di]
  243.     test    si,001
  244.     jz    compute_free_2
  245.     shr    di,1
  246.     shr    di,1
  247.     shr    di,1
  248.     shr    di,1
  249. compute_free_2:
  250.     and    di,0fffh        ;seperate out the bits.
  251.     jnz    compute_free_3        ;free? no.
  252.     inc    ax            ;count a free cluster.
  253. compute_free_3:
  254.     inc    si            ;go to the next.
  255.     loop    compute_free_1        ;until we've looked at them all.
  256.     mov    bx,ax            ;save the free clusters in bx.
  257.     pop    cx            ;restore bytes/sector
  258.     pop    ax            ;restore sectors/cluster.
  259.     pop    ds
  260. compute_free_4:
  261.     clc
  262.     ret
  263.  
  264.  
  265. code    ends
  266.  
  267.     end
  268.  
  269.